<?php
namespace Home\Org\Util;
class GtBuyer
{
    static private $_instance = null;
    private $model;

    private $productManagerList;
    private $productManagerListDetail;

    static public function getInstance()
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new GtBuyer();
        }
        return self::$_instance;
    }

    public function getModel()
    {
        return $this->model;
    }

    private function __construct()
    {
        $this->model = M('gtBuyer');
        $resultArray = $this->model->field("emp_no,captain_no,the_role")->select();
        $this->productManagerListDetail = $resultArray;
        foreach ($resultArray as $key => $value){
            $this->productManagerList[] = $value['emp_no'];
        }
    }


    public function getProductManagerList()
    {
        return $this->productManagerList;
    }

    /**
     * @return mixed
     */
    public function getProductManagerListDetail()
    {
        return $this->productManagerListDetail;
    }

    public function getAllSublist($captain_no)
    {
        $captain_no = strtolower($captain_no);
        $userList = "";
        foreach ($this->productManagerListDetail as $key => $item)
        {
            if($item['captain_no'] == $captain_no)
            {
                //$userList .= '\''.$item['emp_no'].'\'' . ',';

                $userList .= $item['emp_no'] . ',';

                // recursion
                $userList .= $this->getAllSublist($item['emp_no']);

            }
        }
        return $userList;
    }

    public function getAllList($captain_no)
    {
        $captain_no = strtolower($captain_no);
        $userList = "";
        $userList .= $this->getAllSublist($captain_no);
        //$userList .= '\''.$captain_no.'\'';
        $userList .= $captain_no;
        return $userList;
    }

    /**
     * @param $emp_no
     * @param int $type=1 经理， $type=2 总监
     * @return string
     */
    public function getAllCaptain($emp_no,$type=1)
    {
        $emp_no = strtolower($emp_no);
        $userList = "";
        foreach ($this->productManagerListDetail as $key => $item)
        {
            if($item['emp_no'] == $emp_no)
            {

                if($item['captain_no'] && $item['the_role'] == $type)
                    $userList .= $item['captain_no']; //. ',';

                // recursion
                $userList .= $this->getAllCaptain($item['captain_no'],$type);

            }
        }
        return $userList;
    }

}